package com.hrpmar.service.impl;

import com.hrpmar.controller.dto.MessageSenderDto;
import com.hrpmar.mapper.MessageReceiverMapper;
import com.hrpmar.mapper.MessageReplyMapper;
import com.hrpmar.mapper.MessageSenderMapper;
import com.hrpmar.pojo.MessageReply;
import com.hrpmar.pojo.Worker;
import com.hrpmar.service.MessageSenderService;
import com.hrpmar.utils.Constants;
import com.hrpmar.utils.Pager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by c on 2017-12-04.
 */
@Service
@Transactional
public class MessageSenderServiceImpl implements MessageSenderService {

    @Resource
    private MessageSenderMapper messageSenderMapper;

    @Resource
    private MessageReplyMapper messageReplyMapper;

    @Resource
    private MessageReceiverMapper messageReceiverMapper;


    @Override
    public Pager search(Integer offset, Integer limit, MessageSenderDto condition, HttpServletRequest req) {
        HttpSession session  = req.getSession();
        Worker wo = (Worker)session.getAttribute(Constants.LOGIN_USER);

        condition.setSendWorker(wo.getWoJubnum());
        condition.setRecWorker(wo.getWoJubnum());
        Pager pager = new Pager();
        pager.setRows(messageSenderMapper.search(offset,limit,condition));
        pager.setTotal(messageSenderMapper.getTotal(condition));
        return pager;
    }

    @Override
    public List<MessageReply> searchHuiFu(Integer mid) {
        return messageReplyMapper.searchHuiFu(mid);
    }

    @Override
    public MessageReply addHuiFu(MessageReply messageReply,HttpServletRequest req) {
        Worker worker = (Worker)req.getSession().getAttribute(Constants.LOGIN_USER);
        messageReply.setWoJubnum(worker.getWoJubnum());
        messageReply.setReDate(new Date());
        messageReplyMapper.addmessageReply(messageReply);
        MessageReply mess = messageReplyMapper.searchHuiFuById(messageReply.getReid());
        return mess;
    }

    @Override
    public void updateIsReader(Integer rid) {
        messageReceiverMapper.updaIsReader(rid);
    }
}
